package com.isoft.c2team3service2.controller;

import cn.hutool.core.util.StrUtil;
import com.isoft.c2team3service2.bean.ResponseData;
import com.isoft.c2team3service2.entity.UserDTO;
import com.isoft.c2team3service2.service.UserService;
import com.isoft.c2team3service2.util.UserHolder;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.Map;

import static com.isoft.c2team3service2.util.RedisConstants.USER_LOGIN_KEY;

@RestController
@RequestMapping("/login")
public class UserController extends BaseController{
    @Autowired
    UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
     //判断邮箱是否重复
    @GetMapping("/repeat")
    public ResponseData<String> repeat(String email){
       return resultT(userService.repeatEmail(email),"查找");

    }
    //用户登录
    @PostMapping("/name")
        public ResponseData<UserDTO>login(String email, String password, HttpServletResponse response ) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        if (email == null || password == null) {
            return new ResponseData<>(5001, "用户登录信息不完整", null);
        }

        return userService.login(email,password,response);
    }
    //修改密码
    @PutMapping("/repassword")
    public ResponseData<Boolean>repassword(String email, String password) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        boolean b=userService.updatePassword(email, password);
        return resultBoolean(b,"修改密码");
    }

    // 注册
    @PostMapping("/register")
    @ResponseBody
    public Map<String, Object> register(
            @RequestParam String email,
            @RequestParam String code,
            @RequestParam String password,
            @RequestParam Integer roleId
    ) {
        return userService.register(email,code,password ,roleId);
    }

    //查询头像email
    @GetMapping("/rephoto")
    public String upload(String email) {
        return userService.upLode(email);
    }

    //查询头像id
    @GetMapping("/rephotoid")
    public String uploadid(Integer userId) {
        return userService.upLodeId(userId);
    }

    //上传背景图
    @PostMapping("/upload")
    public String upload(MultipartFile file) {
        return userService.upload(file);
    }

    //登出
    @PostMapping("/logout")
    public ResponseData<String> logout(HttpServletResponse response,
                                       @RequestHeader(value = "Authorization", required = false) String token) {
        if (StrUtil.isBlank(token)) {
            return new ResponseData<>(400, "缺少token参数", null);
        }
       String  token1 = token.replace("Bearer ", "");

        String redisKey = USER_LOGIN_KEY + token1;

        Boolean deleted = stringRedisTemplate.opsForHash().getOperations().delete(redisKey);

        UserHolder.removeUser();

        response.setHeader("Authorization", "");

        if (deleted) {
            return new ResponseData<>(200, "登出成功", "" +LocalDateTime.now());
        } else {
            return new ResponseData<>(400, "token无效或已过期", "" +LocalDateTime.now());
        }
    }
}
